package com.handyshop.mall.user.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import com.handyshop.mall.basic.core.vo.ResultVo;
import com.handyshop.mall.dubbo.user.dto.menu.SysMenuAddDto;
import com.handyshop.mall.dubbo.user.dto.menu.SysMenuEditDto;
import com.handyshop.mall.dubbo.user.entity.SysMenu;
import com.handyshop.mall.user.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author hs
 * @date 2020/11/30
 */
@RestController
@RequestMapping("/sysMenu")
@RequiredArgsConstructor
@Api(tags = "菜单模块")
public class SysMenuController {
    private final SysMenuService sysMenuService;

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询")
    public ResultVo<SysMenu> getById(@PathVariable Long id) {
        return ResultVo.success(sysMenuService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "新增菜单")
    //@PreAuthorize("@pms.hasPermission('sys_menu_add')")
    public ResultVo<Boolean> save(@Validated @RequestBody SysMenuAddDto sysMenuAddDto) {
        SysMenu sysMenu = BeanUtil.copyProperties(sysMenuAddDto, SysMenu.class);
        return ResultVo.success(sysMenuService.save(sysMenu));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除")
    //@PreAuthorize("@pms.hasPermission('sys_menu_del')")
    public ResultVo<Boolean> removeById(@PathVariable Long id) {
        return ResultVo.success(sysMenuService.delById(id));
    }

    @PutMapping
    @ApiOperation(value = "根据id编辑")
    //@PreAuthorize("@pms.hasPermission('sys_menu_edit')")
    public ResultVo<Boolean> update(@Validated @RequestBody SysMenuEditDto sysMenuEditDto) {
        SysMenu sysMenu = BeanUtil.copyProperties(sysMenuEditDto, SysMenu.class);
        return ResultVo.success(sysMenuService.updateById(sysMenu));
    }

    @GetMapping("/getMenu")
    @ApiOperation(value = "获取菜单树")
    @ApiImplicitParam(name = "parentId", value = "父级id")
    public ResultVo<List<Tree<Long>>> getMenuTree(Long parentId) {
        return ResultVo.success(sysMenuService.getMenuTree(parentId));
    }

    @GetMapping("/getUserMenu")
    @ApiOperation(value = "获取当前用户的菜单树")
    @ApiImplicitParam(name = "roleId", value = "角色id")
    public ResultVo<List<Tree<Long>>> getUserMenu(Long roleId) {
        return ResultVo.success(sysMenuService.getUserMenuTree(roleId));
    }

    @GetMapping("/getUserMenuId")
    @ApiOperation(value = "获取当前用户的菜单ids")
    @ApiImplicitParam(name = "roleId", value = "角色id")
    public ResultVo<List<Long>> getUserMenuId(Long roleId) {
        return ResultVo.success(sysMenuService.getUserMenuIds(roleId));
    }

}
